<?php
/**
 * Created by PhpStorm.
 * User: fengyan
 * Date: 17-11-3
 * Time: 下午2:47
 */

namespace App\Http\Controllers\Admin\Portal;


use App\Exceptions\MyException;
use App\Http\Controllers\Admin\AdminController;
use App\Repositories;
use Illuminate\Http\Request;

class CategoryController extends AdminController
{
    /**
     * 初始化操作
     * UserController constructor.
     */
    public function __construct()
    {
        parent::__construct();

        $this->setTemplatePrefix(config('admin.systems.template_prefix', 'admin.').'portal.category.');
    }

    /**
     * 获取当前控制器服务仓库对象
     * @return mixed
     */
    public function getServiceRepository(): Repositories\Portal\CategoryRepository
    {
        return Repositories\Portal\CategoryRepository::getInstance();
    }

    /**
     * 列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {
        $pid = $request->route('pid');

        $pid = empty($pid) ? 0 : $pid;

        $pDetail = $this->getServiceRepository()->getDetailById($pid);

        $data = $this->getServiceRepository()->getAdminPaginateData($pid);

        return $this->fetch('index', ['data'=>$data, 'pid'=>$pid, 'pDetail'=>$pDetail]);
    }

    /**
     * 更新栏目
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function edit(Request $request)
    {
        if (strtolower($request->method()) !== 'post') {
            $id = $request->route('id');
            $pid = $request->route('pid');
            $categoryList = $this->getServiceRepository()->getCategoryList(null, null)->toArray();

            $detail = $this->getServiceRepository()->getDetailById($id);
            $action = 'add';
            if (!empty($id)) {
                $action = 'edit';
            }

            return $this->fetch('edit', [
                'metaTitle' => '更新栏目信息',
                'postUrl'   => get_admin_url('portal/category/edit'),
                'detail'    => $detail,
                'action'    => $action,
                'categoryList' => $categoryList,
                'pid'       => $pid
            ]);
        }

        $info = $request->input('info', []);
        $cover = get_value($info, 'cover', []);
        $info['cover'] = get_value($cover, 'url', '');
        $info['cover_id'] = get_value($cover, 'id', 0);
        if ($info['is_page']) {
            $info['document_type'] = 'page';
        }
        $info['template_index'] = !empty($info['template_index']) ? $info['template_index'].$info['document_type'] : '';
        $info['template_list'] = !empty($info['template_list']) ? $info['template_list'].$info['document_type'] : '';
        $info['template_detail'] = !empty($info['template_detail']) ? $info['template_detail'].$info['document_type'] : '';

        if (empty(get_value($info, 'title', ''))) {
            throw new MyException("标题不能为空");
        }

        $action = $request->input('action', 'add');

        $model = $this->getServiceRepository()->save($info, $action);

        //  如果是单网页则保存单网页信息
        if ($info['is_page']) {
            $pageInfo = Repositories\Portal\DocumentRepository::getInstance()->getPageByCategoryId($model->id);
            $docId = empty($pageInfo) ? 0 : $pageInfo->id;
            $document = [
                'id' => $docId,
                'title' => get_value($model,'title', ''),
                'description' => get_value($model,'description', ''),
                'click' => 0,
                'document_type' => 'page',
                'category_id' => $model->id,
                'is_audit' => 99,
                'fixed_release_time' => date('Y-m-d H:i:s'),
            ];
            $extend = [
                'id' => $docId,
                'seo_title' => get_value($model,'seo_title', ''),
                'seo_description' => get_value($model,'seo_description', ''),
                'seo_keywords' => get_value($model,'seo_keywords', ''),
                'template_detail' => get_value($model,'template_detail', ''),
                'content' => '',
                'pic_text' => [],
            ];
            Repositories\Portal\DocumentRepository::getInstance()->saveData($document, $extend, 'page', $action);
        }

        return $this->success('栏目保存成功');
    }
}